app.controller('TurntablCtrl', ['$scope', '$q', 'apiService',
    function(scope, $q, apiService) {
		scope.picurl='';
		var running = false;
		var minPoint = 100;
		scope.memberLoading=true;
		
		var map = {1:320, 2:260, 3:200, 4:140, 5:80, 6:20};
		
		scope.levelMap = {1:'一', 2: '二', 3: '三', 4 : '四', 5 :'五', 6 : '六'};
		
		var init = function(){
			loadMember();
			loadPrizes();
		};
		
		scope.decodeURI=function(str){
			return decodeURI(str);
		}
		
		var loadPrizes = function() {
			apiService.get("/double11/draw/top5").then(function(resp){
				if (resp && resp.data) {
					scope.prizes = resp.data;
					console.log(scope.prizes);
					
					var $list = $('.lucky-list');
			          var $inner = $('.lucky-list-inner');
			          var time = ($inner.innerWidth() / $list.innerWidth() * 6).toFixed(0) + 's';
			          $inner.css({
			            '-webkit-animation-duration': '15s',
			            '-moz-animation-duration': '15s',
			            '-o-animation-duration':'15s',
			            'animation-duration': '15s',
			          });
				} else {
					// todo
				}
			}, function(error){
				console.log(error);
			});
			 
		};
		
		var loadMember = function() {
			apiService.get("/api/member").then(function(resp){
				if (resp && resp.data && resp.data.meta && resp.data.meta.success) {
					scope.member = resp.data.data;
				} else {
					// todo
				}
				scope.memberLoading=false;
			}, function(error){
				console.log(error);
				scope.memberLoading=false;
			});
			
			apiService.get("/api/member/img").then(function(resp){
				if (resp && resp.data && resp.data.data) {
					scope.picurl = resp.data.data;	
					console.log(resp.data.data);
				} else {
					// todo
				}
			}, function(error){
				console.log(error);
			});
		};
		
		scope.luckydraw = function() {
			if(scope.memberLoading){
				return;
			}
			if (!scope.member || !scope.member.tel) {
				$('.alert-error button').click(function(){
					window.location.href = "https://siemens.ews.m.jaeapp.com/profile/"+$("#stid").val();
				});
				showErrorMsg("请先完善个人信息");
				return;
			}
			if (running) {
				return ;
			}
			running = true;
			
			if (scope.member && scope.member.points && scope.member.points[0].totalActivePoints >= minPoint) {
				emptyRotate();
				apiService.get("/double11/draw").then(function(resp){
					if (resp && resp.data && resp.data.success) {
						scope.member.points[0].totalActivePoints-=100;
						setTimeout(function(){loadMember();},4000);
						var level = resp.data.prizeLevel;
						rotateFunc(level, map[level], "恭喜您中了" + scope.levelMap[level]+ "等奖" +".西门子家电5周年庆,大额优惠等你来!");
						$('.alert button').click(function(){
							window.location.href = "https://siemens.ews.m.jaeapp.com/load/index/1";
						});
					} else {
						running = false;
						if (resp && resp.data && resp.data.message) {
							showErrorMsg("抽奖失败：" + resp.data.message);
						} else {
							showErrorMsg("抽奖人数爆满,请稍后再试!");
						}
						$rotateBody.stopRotate();
					}
				}, function(error){
					running = false;
					showErrorMsg("抽奖人数爆满,请稍后再试!");
					$rotateBody.stopRotate();
				});
				
			} else {
				running = false;
				showErrorMsg("你的抽奖积分不足，不能抽奖")
				return;
			}
		};
		
		 var $plateBtn = $('#plateBtn');
         var $rotateBody = $('#rotateBody');

		var emptyRotate = function() {
			$rotateBody.stopRotate();
			$rotateBody.rotate({
				angle : 0,
				duration : 1000 * 60,
				animateTo : 360 * 60,
				callback : function() {
					showErrorMsg("亲,双11压力山大!", true);
				}
			});
		};
		
		var rotateFunc = function(awards, angle, text) { // awards:奖项，angle:奖项对应的角度
			$rotateBody.stopRotate();
			$rotateBody.rotate({
				angle : 0,
				duration : 5000,
				animateTo : angle + 1440, // angle是图片上各奖项对应的角度，1440是让指针固定旋转4圈
				callback : function() {
					showMsg(text, true);
					running = false;
				}
			});
		};
		
		var showMsg = function(msg, apply) {
			scope.msg = msg;
			if (apply) {
				scope.$apply();
			}
			if ($('.alert').hasClass('hide')) {
				$('.alert').removeClass('hide');
			}
			
		};
		
		var showErrorMsg = function(msg, apply) {
			scope.msg = msg;
			if (apply) {
				scope.$apply();
			}
			if ($('.alert-error').hasClass('hide')) {
				$('.alert-error').removeClass('hide');
			}
			
		};
		
		init();
		
	}
]);

$('.alert button').click(function() {
	if (!$('.alert').hasClass('hide')) {
		$('.alert').addClass('hide');
	}
});

$('.alert-error button').click(function() {
	if (!$('.alert-error').hasClass('hide')) {
		$('.alert-error').addClass('hide');
	}
});


